﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections.Generic;
using Dcms.Common;
using AjaxPro;
using System.Text.RegularExpressions;

public partial class Manage_Modle_UploadFile_ListFiles : System.Web.UI.Page
{

    //取得附件虚拟路径
    private readonly string strDummyPath = ConfigurationManager.AppSettings["UploadFilePath"];
    private int intIndex = 0;
    private List<DirectoryIntro> lDirectoryInfo = new List<DirectoryIntro>();

    /// <summary>
    /// 页面加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        Utility.RegisterTypeForAjax(typeof(Manage_Modle_UploadFile_ListFiles));
        if (!IsPostBack)
        {
        }
    }

    /// <summary>
    /// 取得第一层目录
    /// </summary>
    /// <returns></returns>
    [AjaxMethod()]
    public string GetRoot()
    {
        string strPath = Server.MapPath(strDummyPath);
        int intCurrID = 0;
        int intDirIndex = intCurrID;
        DirectoryList DList = new DirectoryList();
        DList.ID = intCurrID;
        DList.Name = "";
        DList.ParentID = intCurrID;
        DList.Path = "";

        //取文件夹列表
        string[] arrDirectory = Directory.GetDirectories(strPath);
        foreach (string strDirectory in arrDirectory)
        {
            intDirIndex++;
            DirectoryIntro1 etDirectoryIntro = new DirectoryIntro1();
            etDirectoryIntro.ID = intDirIndex;
            etDirectoryIntro.Name = strDirectory.Replace(strPath, "");
            if (Directory.GetDirectories(strDirectory).Length > 0)
            {
                etDirectoryIntro.IsHasChild = true;
            }
            else
            {
                etDirectoryIntro.IsHasChild = false;
            }
            DList.DirListAdd(etDirectoryIntro);
        }

        //取文件
        int intFileIndex = 0; 
        DirectoryInfo info = new DirectoryInfo(strPath);
        FileInfo[] arrFiles = info.GetFiles();

        //进行冒泡排序
        FileInfo FileInfoTemp;
        for (int k = 0; k < arrFiles.Length; k++)
        {
            for (int j = k + 1; j < arrFiles.Length; j++)
            {
                if (DateTime.Compare(arrFiles[k].CreationTime, arrFiles[j].CreationTime) < 0)
                {
                    FileInfoTemp = arrFiles[k];
                    arrFiles[k] = arrFiles[j];
                    arrFiles[j] = FileInfoTemp;
                }
            }
        }

        foreach (FileInfo file in arrFiles)
        {
            intFileIndex++;
            FileIntro1 etFileIntro = new FileIntro1();
            etFileIntro.ID = intFileIndex;
            etFileIntro.Name = file.Name;
            etFileIntro.Size = 0;
            etFileIntro.Type = "jpg";
            DList.FileListAdd(etFileIntro);
        }

        return "this.DiryList[" + intCurrID + "]=" + JsonHelper.ToJson(DList);
    }


    [AjaxMethod()]
    public string GetSubDirectoryList(int intDirID, string strDirName, int intParentID, string strParentPath, int intDirCount)
    {
        string strPath = Server.MapPath(strDummyPath + "\\" + strParentPath + "\\");
        DirectoryList DList = new DirectoryList();
        DList.ID = intDirID;
        DList.Name = strDirName;
        DList.ParentID = intDirID;
        DList.Path = strParentPath;

        //取文件夹列表
        string[] arrDirectory = Directory.GetDirectories(strPath);
        foreach (string strDirectory in arrDirectory)
        {
            intDirCount++;
            DirectoryIntro1 etDirectoryIntro = new DirectoryIntro1();
            etDirectoryIntro.ID = intDirCount;
            etDirectoryIntro.Name = strDirectory.Replace(strPath, "");
            if (Directory.GetDirectories(strDirectory).Length > 0)
            {
                etDirectoryIntro.IsHasChild = true;
            }
            else
            {
                etDirectoryIntro.IsHasChild = false;
            }
            DList.DirListAdd(etDirectoryIntro);
        }

        //取文件
        int intFileIndex = 0;
        DirectoryInfo info = new DirectoryInfo(strPath);
        FileInfo[] arrFiles = info.GetFiles();

        //进行冒泡排序
        FileInfo FileInfoTemp;
        for (int k = 0; k < arrFiles.Length; k++)
        {
            for (int j = k + 1; j < arrFiles.Length; j++)
            {
                if (DateTime.Compare(arrFiles[k].CreationTime, arrFiles[j].CreationTime) < 0)
                {
                    FileInfoTemp = arrFiles[k];
                    arrFiles[k] = arrFiles[j];
                    arrFiles[j] = FileInfoTemp;
                }
            }
        }

        foreach (FileInfo file in arrFiles)
        {
            intFileIndex++;
            FileIntro1 etFileIntro = new FileIntro1();
            etFileIntro.ID = intFileIndex;
            etFileIntro.Name = file.Name;
            etFileIntro.Size = 0;
            etFileIntro.Type = "jpg";
            DList.FileListAdd(etFileIntro);
        }

        return "this.DiryList[" + intDirID + "]=" + JsonHelper.ToJson(DList);
    }

    [AjaxMethod()]
    public string GetDirectoryInfo()
    {
        string strPath = Server.MapPath(strDummyPath);

        //判断当前文件夹是否存在
        if (Directory.Exists(strPath))
        {
            string[] arrDirectories = Directory.GetDirectories(strPath);
            foreach (string strDirectory in arrDirectories)
            {
                string s = strDirectory.Replace(strPath, "");
                intIndex++;
                DirectoryIntro etDirectoryInfo = new DirectoryIntro();
                etDirectoryInfo.ID = intIndex;
                etDirectoryInfo.Name = s;
                etDirectoryInfo.Path = strDirectory.Replace(strPath, "");
                if (Directory.GetDirectories(strDirectory).Length > 0)
                {
                    etDirectoryInfo.IsChild = true;
                }
                else
                {
                    etDirectoryInfo.IsChild = false;
                }
                GetDirectories1(etDirectoryInfo, strDirectory);
                lDirectoryInfo.Add(etDirectoryInfo);
            }
        }
        return "var DirList=" + JsonHelper.ToJson(lDirectoryInfo);
    }

    private void GetDirectories1(DirectoryIntro etDirectoryInfo, string strPath)
    {
        string strRootPath = Server.MapPath(strDummyPath);
        //判断当前文件夹是否存在
        if (Directory.Exists(strPath))
        {
            string[] arrDirectories = Directory.GetDirectories(strPath);
            foreach (string strDirectory in arrDirectories)
            {
                string s = strDirectory.Replace(strPath + "\\", "");
                intIndex++;
                DirectoryIntro etDirectoryInfo1 = new DirectoryIntro();
                etDirectoryInfo1.ID = intIndex;
                etDirectoryInfo1.Name = s;
                etDirectoryInfo1.Path = strDirectory.Replace(strRootPath, "").Replace("\\", "\\\\");
                if (Directory.GetDirectories(strDirectory).Length > 0)
                {
                    etDirectoryInfo1.IsChild = true;
                }
                else
                {
                    etDirectoryInfo1.IsChild = false;
                }
                etDirectoryInfo.SetParent(etDirectoryInfo1);
                GetDirectories1(etDirectoryInfo1, strDirectory);
            }
        }
    }

    /// <summary>
    /// 列出对应目录下的文件
    /// </summary>
    /// <param name="strPath">目录路径</param>
    /// <param name="intDirIndex">目录编号</param>
    /// <returns></returns>
    [AjaxMethod()]
    public string GetFileInfo(string strPath, int intDirIndex)
    {
        int intFileIndex = 0;
        string strRootPath = Server.MapPath(strDummyPath);
        string strFullPath = Server.MapPath(strDummyPath + "\\" + strPath);
        List<FileIntro> lFileIntro = new List<FileIntro>();
        DirectoryInfo info = new DirectoryInfo(strFullPath);
        FileInfo[] arrFiles = info.GetFiles();

        //进行冒泡排序
        FileInfo FileInfoTemp;
        for (int k = 0; k < arrFiles.Length; k++)
        {
            for (int j = k + 1; j < arrFiles.Length; j++)
            {
                if (DateTime.Compare(arrFiles[k].CreationTime, arrFiles[j].CreationTime) < 0)
                {
                    FileInfoTemp = arrFiles[k];
                    arrFiles[k] = arrFiles[j];
                    arrFiles[j] = FileInfoTemp;
                }
            }
        }

        foreach (FileInfo file in arrFiles)
        {
            FileIntro etFileIntro = new FileIntro();
            etFileIntro.ID = intFileIndex;
            etFileIntro.Name = file.Name;
            etFileIntro.Size = 0;
            etFileIntro.Type = "jpg";
            lFileIntro.Add(etFileIntro);
            intFileIndex++;
        }

        return "FileList[" + intDirIndex.ToString() + "]=" + JsonHelper.ToJson(lFileIntro);
    }

    /// <summary>
    /// 删除文件
    /// </summary>
    /// <param name="strFilePath"></param>
    /// <returns></returns>
    [AjaxMethod()]
    public string DeleteFileInfo(string strFilePath)
    {
        string strFullPath = Server.MapPath(strDummyPath + "\\" + strFilePath);
        try
        {
            File.Delete(strFullPath);
            return "1";
        }
        catch { }
        return "0";
    }
}
